const db = wx.cloud.database({
  env: "cloudserver-7geqn70y75cce339"
})
const user = db.collection('user')
const plist = db.collection('plist')
// pages/demo/demo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    imgSrc: "../../img/idea.png",
    shareSrc: '../../img/share.png',
    likeSrc: '../../img/like.png',
    standTxt: [{
        stand: "1",
        txt: "近战位"
      },
      {
        stand: "2",
        txt: "远程位"
      }
    ],
    standnum:"",
    sexTxt: [{
        sex: "1",
        txt: "男性干员"
      },
      {
        sex: '2',
        txt: "女性干员"
      }
    ],
    sexnum:"",
    chaTxt: [{
        cha: "1",
        txt: "支援机械",
      },
      {
        cha: "2",
        txt: "输出"
      },
      {
        cha: "3",
        txt: "防护"
      }, {
        cha: "4",
        txt: "治疗"
      }, {
        cha: "5",
        txt: "费用回复"
      },
      {
        cha: "6",
        txt: "群攻"
      },
      {
        cha: "7",
        txt: "减速"
      }
    ],
    chanum:"",
    occTxt: [{
        occ: "1",
        txt: "先锋干员"
      },
      {
        occ: "2",
        txt: "狙击干员"
      }, {
        occ: "3",
        txt: "医疗干员"
      },
      {
        occ: "4",
        txt: "术士干员"
      }, {
        occ: "5",
        txt: "近卫干员"
      }, {
        occ: "6",
        txt: "重装干员"
      }, {
        occ: "7",
        txt: "辅助干员"
      }
    ],
    occnum:"",
    database:[{
      stand:""
    },{
      sex:""
    },{
      occ:""
    },{
      cha:""
    }],
    pdata:""
  },
  //测试用
  // searchUser() {
  //   let id = "9890f4c6635519b4001c7db67d7f4425";
  //   user.get().then(res => {
  //     console.log(res);
  //     this.setData({
  //       name: res.data.userName
  //     })
  //   })
  // },
  //获取站位
  standMes(event){
    let stand = this.data.database[0].stand;
    // console.log(stand);
    let data = this.data.database;
    // console.log(data);
    let sID = event.target.dataset.stand;
    if(stand){
      if(stand == sID){
        stand = "";
        data[0].stand = stand;
        this.setData({
          database:data,
          standnum:stand
        })
      }else{
        stand = sID
        data[0].stand = stand;
        this.setData({
          database:data,
          standnum:stand
        })
      }
    }else{
      stand = sID;
      data[0].stand = stand
      this.setData({
        database:data,
        standnum:stand
      })
    }
    this.getData();
    // console.log("database",this.data.database);
  },
  //获取性别
  sexMes(event){
    let sex = this.data.database[1].sex;
    let data = this.data.database;
    let sexID = event.target.dataset.stand;
    // console.log(sex);
    // console.log(data);
    // console.log(sexID);
    if(sex){
      if(sex == sexID){
        sex = "";
        data[1].sex = sex;
        this.setData({
          database:data,
          sexnum:sex
        })
      }else{
        sex = sexID
        data[1].sex = sex;
        this.setData({
          database:data,
          sexnum:sex
        })
      }
    }else{
      sex = sexID;
      data[1].sex = sex
      this.setData({
        database:data,
        sexnum:sex
      })
    }
    this.getData();
    // console.log("database",this.data.database);
  },
  //获取职业
  occMes(event){
    let occ = this.data.database[2].occ;
    let data = this.data.database;
    let occID = event.target.dataset.stand;
    // console.log(sex);
    // console.log(data);
    // console.log(sexID);
    if(occ){
      if(occ == occID){
        occ = "";
        data[2].occ = occ;
        this.setData({
          database:data,
          occnum:occ
        })
      }else{
        occ = occID
        data[2].occ = occ;
        this.setData({
          database:data,
          occnum:occ
        })
      }
    }else{
      occ = occID;
      data[2].occ = occ
      this.setData({
        database:data,
        occnum:occ
      })
    }
    this.getData();
    // console.log("database",this.data.database);
  },
  //获取特性
  chaMes(event){
    let cha = this.data.database[3].cha;
    let data = this.data.database;
    let chaID = event.target.dataset.stand;
    // console.log(cha);
    // console.log(data);
    // console.log(sexID);
    if(cha){
      if(cha == chaID){
        cha = "";
        data[3].cha = cha;
        this.setData({
          database:data,
          chanum:cha
        })
      }else{
        cha = chaID
        data[3].cha = cha;
        this.setData({
          database:data,
          chanum:cha
        })
      }
    }else{
      cha = chaID;
      data[3].cha = cha
      this.setData({
        database:data,
        chanum:cha
      })
    }
    this.getData();
    // console.log("database",this.data.database);
  },
  //连接数据库并进行条件筛选渲染干员
  getData(){
    wx.showLoading({
      title: '加载中',
    })
    let stand = this.data.database[0].stand;
    let sex = this.data.database[1].sex;
    let occ = this.data.database[2].occ;
    let cha = this.data.database[3].cha;
    console.log(cha);
    if(stand == ""){
      stand = undefined;
    }
    if(sex == ""){
      sex = undefined;
    }
    if(occ == ""){
      occ = undefined;
    }
    if(cha == ""){
      cha = undefined;
    }
    wx.cloud.database().collection('plist').where({
      standid:stand,
      sexid:sex,
      chaid:cha,
      occid:occ,
    }).get().then(res=>{
      // console.log(res);
      this.setData({
        pdata:res.data
      })
      wx.hideLoading()
    }).catch(
    )
  },
  //初始化干员列表
  baseGet(){
    plist.get().then(res =>{
      // console.log("1",res);
      // console.log(this);
      this.setData({
        pdata:res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.searchUser()
    this.getData()
    this.baseGet()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})